<?php
/***************************************************************************
 * @Description: 从feed导入文档
 * 
 * @FileName:         FeedController.php
 * @Author :          liyong5@staff.sina.com.cn
 * @CreatedTime:      2018年07月10日 星期二 01时12分31秒
 ************************************************************************/

namespace app\modules\core\console;

use Yii;
use yii\console\Controller;
use yii\httpclient\Client;
use yii\httpclient\Request;

class FeedController extends  Controller
{
    const FEED_URL = 'http://i.feed.mix.sina.com.cn/api/roll/get';

    private static $httpClient;

    private static $feedList = [
        'nba'       => '653',
        'football'  => '654',
        'cfootball' => '655',
        'cba'       => '656',
        'other'     => '657',
    ];

    private $params = [
        'pageid'    => 40,
        'page_type' => 2,
        'lid'       => 653,
        'feed_id'   => 653,
        'num'       => 50,
        'stime'     => '',
        'etime'     => '',
        'fields'    => 'title,url,img,categoryid,comment_total,comment_show,ctime,video_info,docid,video_id,commentid,keywords,intro'
    ];

    public function actionImport() {
        $stime = time();
        $etime = $stime - 11 * 60;

        echo "from {$stime} to {$etime} ............................................................", PHP_EOL;


        foreach (self::$feedList as $sport => $feedId) {
            $this->params['lid'] = $feedId;
            $this->params['feed_id'] = $feedId;
            $this->params['stime'] = $stime;
            $this->params['etime'] = $etime;

            $url = self::FEED_URL . http_build_query($this->params);
            $this->import($url);
        }

        
        echo 'end....................................................................................', PHP_EOL;
    }

    private function import($url) {
        $request = $this->getHttpClient()->createRequest()
            ->setUrl($url)
            ->setMethod('get')
            ->setData([]);

        $response = $request->send();

        if (! $response->getIsOk()) {
            return;
        }

        $data = $response->getData();
        if (0 != $data['result']['status']['code']) {
            return;
        }
        if (empty($data['result']['data'])) {
            return;
        }

        foreach ($data['result']['data'] as $new) {
            $new['ctime'] = $new['ctime'] * 1000;
            // format
            $this->syncEs('PUT', $new['docid'], $new);
        }
    }

    private function syncEs($method, $id, $doc) {
        $request = $this->getHttpClient()->createRequest()
            ->setUrl("http://localhost:9200/feeds/nba/{$id}")
            ->setMethod($method)
            ->setFormat(Client::FORMAT_JSON)
            ->setData($doc);

        $response = $request->send();

        echo sprintf("docid:%s status:%d", $id, $response->getStatusCode()), PHP_EOL;
        return $response->getIsOk();
    }

    private static function getHttpClient() {
        if (self::$httpClient && self::$httpClient instanceof Client) {
            return self::$httpClient;
        }
        return self::$httpClient = new Client();
    }
}
